//
//  TabBarView.m
//  PerfectProject
//
//  Created by Meng huan on 14/12/12.
//  Copyright (c) 2014年 M.H Co.,Ltd. All rights reserved.
//

#import "TabBarView.h"

// App下导航栏页数
#define DEF_TAB_ITEM_COUNT 4
@interface TabBarView()
{
    NSMutableArray * textArray;
    NSMutableArray * btnArray;
}
@end
@implementation TabBarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor    = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tab_bg"]];
        self.backgroundColor    = [UIColor whiteColor];
//        self.layer.borderWidth  = 1;
//        self.layer.borderColor  = [[UIColor grayColor] colorWithAlphaComponent:0.3].CGColor;
        
        // tabBar上每个item的宽度
        float itemWidth = DEF_SCREEN_WIDTH/DEF_TAB_ITEM_COUNT;
        
        //
        NSArray *btnNameArray = [NSArray arrayWithObjects:@"首页", @"安全", @"发现", @" 我", nil];
        textArray = [NSMutableArray array];
        btnArray = [NSMutableArray array];
        // 创建每个item
        for (int i=0; i<DEF_TAB_ITEM_COUNT; i++)
        {
            UIButton *itemButton                    = [[UIButton alloc] initWithFrame:CGRectMake(itemWidth*i, 0, itemWidth, DEF_HEIGHT(self))];
            itemButton.tag                          = i;
            itemButton.adjustsImageWhenHighlighted  = NO;
            itemButton.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
            itemButton.titleLabel.font = [UIFont systemFontOfSize:10];
            [itemButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_item%d", i]] forState:UIControlStateNormal];
            [itemButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_item%d_select", i]] forState:UIControlStateSelected];
//            [itemButton setTitle:[btnNameArray objectAtIndex:i] forState:UIControlStateNormal];
//            [itemButton setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
//            [itemButton setTitleColor:[UIColor colorWithRed:0 green:0.75 blue:0.93 alpha:1] forState:UIControlStateSelected];
            
            [itemButton addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            [btnArray addObject:itemButton];
            [self addSubview:itemButton];
            
            //标题
            UILabel * textLa = [[UILabel alloc]initWithFrame:CGRectMake(0, DEF_HEIGHT(self)-15, itemWidth, 10)];
            textLa.font = font(10);
            textLa.tag = 10+i;
            textLa.textAlignment = NSTextAlignmentCenter;
            textLa.textColor = [UIColor grayColor];
            textLa.text = [btnNameArray objectAtIndex:i];
            [itemButton addSubview:textLa];
            [textArray addObject:textLa];
            
            if (i == 0) {
                itemButton.selected = YES;
                textLa.textColor = [UIColor colorWithRed:0 green:0.75 blue:0.93 alpha:1];
            }
            
        }
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backOneItem) name:@"logout" object:nil];
    }
    return self;
}

- (void)itemClick:(UIButton *)item
{
    for (UIButton *button in self.subviews)
    {
        button.selected = NO;
    }
    item.selected = YES;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarView:didSelectAtIndex:)]) {
        [self.delegate tabBarView:self didSelectAtIndex:item.tag];
    }
    for (UILabel * label in textArray)
    {
        label.textColor = [UIColor grayColor];
    }
    if (item.selected)
    {
        UILabel * label = (UILabel *)[textArray objectAtIndex:item.tag];
        label.textColor = [UIColor colorWithRed:0 green:0.75 blue:0.93 alpha:1];
    }

}
- (void)backOneItem
{
    for (UIButton * obj in btnArray)
    {
        obj.selected = NO;
    }
    UIButton * btn = (UIButton *)[btnArray objectAtIndex:0];
    btn.selected = YES;
    
    for (UILabel * label in textArray)
    {
        label.textColor = [UIColor grayColor];
    }
    UILabel * label = (UILabel *)[textArray objectAtIndex:0];
    label.textColor = [UIColor colorWithRed:0 green:0.75 blue:0.93 alpha:1];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"logout" object:nil];
}
@end
